package com.sdhs.projectschedule.service.impl;

import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.listmanage.domain.entity.GcManageList;
import com.sdhs.listmanage.mapper.GcManageListMapper;
import com.sdhs.projectschedule.domain.GcProgressSupply;
import com.sdhs.projectschedule.domain.GcProgressSupplyFill;
import com.sdhs.projectschedule.mapper.GcProgressSupplyFillMapper;
import com.sdhs.projectschedule.mapper.GcProgressSupplyMapper;
import com.sdhs.projectschedule.service.IGcProgressSupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目进度控制--供货填报-主Service业务层处理
 *
 * @author ww
 */
@Service
public class GcProgressSupplyServiceImpl implements IGcProgressSupplyService {
    @Autowired
    private GcProgressSupplyMapper gcProgressSupplyMapper;
    @Autowired
    private GcProgressSupplyFillMapper gcProgressSupplyFillMapper;
    @Autowired
    private GcManageListMapper gcManageListMapper;

    /**
     * 查询项目进度控制--供货填报-主
     *
     * @param id 项目进度控制--供货填报-主主键
     * @return 项目进度控制--供货填报-主
     */
    @Override
    public GcProgressSupply selectGcProgressSupplyById(Long id) {
        return gcProgressSupplyMapper.selectGcProgressSupplyById(id);
    }

    /**
     * 查询项目进度控制--供货填报-主列表
     *
     * @param gcProgressSupply 项目进度控制--供货填报-主
     * @return 项目进度控制--供货填报-主
     */
    @Override
    public List<GcProgressSupply> selectGcProgressSupplyList(GcProgressSupply gcProgressSupply) {
        return gcProgressSupplyMapper.selectGcProgressSupplyTreeList(gcProgressSupply);
    }

    /**
     * 新增项目进度控制--供货填报-主
     *
     * @param gcProgressSupply 项目进度控制--供货填报-主
     * @return 结果
     */
    @Override
    public int insertGcProgressSupply(GcProgressSupply gcProgressSupply) {
        if (gcProgressSupply.getId() == null) {
            //新增进度计划主表信息
            gcProgressSupply.setCreateBy(SecurityUtils.getUsername());
            gcProgressSupply.setCreateTime(DateUtils.getNowDate());
            gcProgressSupply.setSupplySchedule(handleScale(gcProgressSupply.getSupplyFillList()));
            gcProgressSupplyMapper.insertGcProgressSupply(gcProgressSupply);
        }
        //新增进度计划子表信息
        gcProgressSupplyFillMapper.deleteGcProgressSupplyFillBySupplyId(gcProgressSupply.getId());
        gcProgressSupply.getSupplyFillList().forEach(progressSupplyFill -> {
            progressSupplyFill.setSupplyId(gcProgressSupply.getId());
            progressSupplyFill.setCreateBy(SecurityUtils.getUsername());
            progressSupplyFill.setCreateTime(DateUtils.getNowDate());
        });
        return gcProgressSupplyFillMapper.insertGcProgressSupplyFillList(gcProgressSupply.getSupplyFillList());
    }

    /**
     * 修改项目进度控制--供货填报-主
     *
     * @param gcProgressSupply 项目进度控制--供货填报-主
     * @return 结果
     */
    @Override
    public int updateGcProgressSupply(GcProgressSupply gcProgressSupply) {
        gcProgressSupply.setUpdateBy(SecurityUtils.getUsername());
        gcProgressSupply.setUpdateTime(DateUtils.getNowDate());
        return gcProgressSupplyMapper.updateGcProgressSupply(gcProgressSupply);
    }

    /**
     * 批量删除项目进度控制--供货填报-主
     *
     * @param ids 需要删除的项目进度控制--供货填报-主主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressSupplyByIds(Long[] ids) {
        return gcProgressSupplyMapper.deleteGcProgressSupplyByIds(ids);
    }

    /**
     * 删除项目进度控制--供货填报-主信息
     *
     * @param id 项目进度控制--供货填报-主主键
     * @return 结果
     */
    @Override
    public int deleteGcProgressSupplyById(Long id) {
        return gcProgressSupplyMapper.deleteGcProgressSupplyById(id);
    }

    private String handleScale(List<GcProgressSupplyFill> supplyFillList) {
        /*判断是否为空然后返回0*/
        if (supplyFillList == null || supplyFillList.isEmpty()) {
            return "0";
        }

        List<GcManageList> gcManageLists = gcManageListMapper.selectGcManageListByProject(supplyFillList.get(0).getProjectId());
        if (gcManageLists == null || gcManageLists.isEmpty()) {
            return "0";
        }
        /*去掉quantity是0或者是空的的子目号*/
        List<GcManageList> filteredManageLists = gcManageLists.stream()
                .filter(item -> {
                    try {
                        double quantity = Double.parseDouble(String.valueOf(item.getQuantity()));
                        return quantity != 0;
                    } catch (NumberFormatException e) {
                        return false;
                    }
                })
                .collect(Collectors.toList());

        double ratioSum = 0;
        for (GcProgressSupplyFill item : supplyFillList) {
            try {
                double supplyQuantity = Double.parseDouble(String.valueOf(item.getSupplyQuantity()));
                double quantity = Double.parseDouble(String.valueOf(item.getQuantity()));
                if (quantity != 0) {
                    ratioSum += supplyQuantity / quantity;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                continue;
            }
        }

        double averageRatio = ratioSum / filteredManageLists.size();
        /*不保留两位小数
        return String.valueOf(averageRatio * 100);*/
        double percentage = averageRatio * 100;
        if(percentage > 100){
            percentage = 100;
        }

        // 使用 DecimalFormat 保留两位小数
        DecimalFormat df = new DecimalFormat("#.##");
        return df.format(percentage); // 例如：85.67
    }

}
